package cn.xzhang.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONUtil;
import cn.xzhang.boot.common.exception.ServiceException;
import cn.xzhang.boot.common.pojo.PageResult;
import cn.xzhang.boot.mapper.WallpaperDetailsMapper;
import cn.xzhang.boot.model.dto.wallpaperDetails.WallpaperDetailsAddReqDTO;
import cn.xzhang.boot.model.dto.wallpaperDetails.WallpaperDetailsPageReqDTO;
import cn.xzhang.boot.model.dto.wallpaperDetails.WallpaperDetailsUpdateReqDTO;
import cn.xzhang.boot.model.entity.WallpaperCategories;
import cn.xzhang.boot.model.entity.WallpaperDetails;
import cn.xzhang.boot.model.vo.wallpaperDetails.WallpaperDetailsSimpleVo;
import cn.xzhang.boot.model.vo.wallpaperDetails.WallpaperDetailsVo;
import cn.xzhang.boot.service.WallpaperCategoriesService;
import cn.xzhang.boot.service.WallpaperDetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.xzhang.boot.common.exception.enums.GlobalErrorCodeConstants.*;
import static cn.xzhang.boot.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 针对表【wallpaperDetails(壁纸表)】的数据库操作Service实现
 *
 * @author <a href="https://github.com/XiaoZhangCode">XiaoZhangCode</a>
 */
@Service
public class WallpaperDetailsServiceImpl extends ServiceImpl<WallpaperDetailsMapper, WallpaperDetails> implements WallpaperDetailsService {

    @Resource
    private WallpaperDetailsMapper wallpaperDetailsMapper;

    @Resource
    private WallpaperCategoriesService wallpaperCategoriesService;


    /**
     * 添加新壁纸
     *
     * @param wallpaperDetailsReqDTO 壁纸信息请求DTO
     * @return 添加成功返回壁纸id
     */
    @Override
    public long addWallpaperDetails(WallpaperDetailsAddReqDTO wallpaperDetailsReqDTO) {
        WallpaperDetails wallpaperDetails = new WallpaperDetails();
        BeanUtil.copyProperties(wallpaperDetailsReqDTO, wallpaperDetails);
        if (!this.save(wallpaperDetails)) {
            throw exception(ADD_FAIL);
        }
        return wallpaperDetails.getId();
    }

    /**
     * 更新壁纸信息
     *
     * @param wallpaperDetailsReqDTO 壁纸信息更新请求DTO
     * @return 更新成功返回true
     */
    @Override
    public boolean updateWallpaperDetails(WallpaperDetailsUpdateReqDTO wallpaperDetailsReqDTO) {
        if (wallpaperDetailsReqDTO.getId() == null) {
            throw exception(BAD_REQUEST);
        }
        WallpaperDetails wallpaperDetails = new WallpaperDetails();
        BeanUtil.copyProperties(wallpaperDetailsReqDTO, wallpaperDetails);
        boolean b = this.updateById(wallpaperDetails);
        if (!b) {
            throw exception(UPDATE_FAIL);
        }
        return true;
    }

    /**
     * 删除壁纸
     *
     * @param id 壁纸id
     * @return 删除成功返回true
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public boolean deleteWallpaperDetails(Long id) {
        if (id == null) {
            throw exception(BAD_REQUEST);
        }
        boolean b = this.removeById(id);
        if (!b) {
            throw exception(DELETE_FAIL);
        }
        return true;
    }

    /**
     * 将壁纸对象转换为壁纸VO对象
     *
     * @param wallpaperDetails 壁纸对象
     * @return 返回壁纸VO对象
     */
    @Override
    public WallpaperDetailsSimpleVo getSimpleWallpaperDetailsVO(WallpaperDetails wallpaperDetails) {
        if (wallpaperDetails == null) {
            return null;
        }
        WallpaperDetailsSimpleVo wallpaperDetailsSimpleVo = new WallpaperDetailsSimpleVo();
        BeanUtil.copyProperties(wallpaperDetails, wallpaperDetailsSimpleVo);
        return wallpaperDetailsSimpleVo;
    }

    /**
     * 获取壁纸分页信息
     *
     * @param wallpaperDetailsPageReqDTO 壁纸分页请求DTO
     * @return 返回壁纸分页结果
     */
    @Override
    public PageResult<WallpaperDetailsVo> getWallpaperDetailsPage(WallpaperDetailsPageReqDTO wallpaperDetailsPageReqDTO) {
        PageResult<WallpaperDetails> pageResult = wallpaperDetailsMapper.selectPage(wallpaperDetailsPageReqDTO);
        if (pageResult.getList() == null) {
            return PageResult.empty();
        }
        // 获取分类id  然后根据分类ID获取分类名称
        Map<Long, String> categoryMap;
        Set<Long> classIds = pageResult.getList().stream().map(WallpaperDetails::getClassId).collect(Collectors.toSet());
        if (CollUtil.isEmpty(classIds)) {
            categoryMap = null;
        } else {
            List<WallpaperCategories> wallpaperCategories = wallpaperCategoriesService.getBaseMapper().selectBatchIds(classIds);
            categoryMap = wallpaperCategories.stream().collect(Collectors.toMap(WallpaperCategories::getId, WallpaperCategories::getName));
        }

        List<WallpaperDetailsVo> wallpaperDetailsVos = pageResult.getList().stream().map(wallpaperDetails -> {
            WallpaperDetailsVo wallpaperDetailsVo = new WallpaperDetailsVo();
            BeanUtil.copyProperties(wallpaperDetails, wallpaperDetailsVo);
            if (ObjUtil.isNotNull(categoryMap) && wallpaperDetails.getClassId() != null) {
                wallpaperDetailsVo.setClassName(categoryMap.getOrDefault(wallpaperDetails.getClassId(), "未知分类"));
            }
            return wallpaperDetailsVo;
        }).collect(Collectors.toList());
        return new PageResult<>(wallpaperDetailsVos, pageResult.getTotal());
    }

    @Override
    public WallpaperDetailsVo getWallpaperDetailsVO(WallpaperDetails wallpaperDetails) {
        if (wallpaperDetails == null) {
            return null;
        }
        WallpaperDetailsVo wallpaperDetailsVo = new WallpaperDetailsVo();
        BeanUtil.copyProperties(wallpaperDetails, wallpaperDetailsVo);
        return wallpaperDetailsVo;
    }

}



